package com.yzxb.messystem.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
    
    private static final Logger logger = LoggerFactory.getLogger(Utils.class);
    
    // 生成UUID
    public static String generateUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }
    
    // 格式化日期
    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
    
    // 格式化当前日期
    public static String formatCurrentDate(String pattern) {
        return formatDate(new Date(), pattern);
    }
    
    // 生成随机数
    public static int generateRandomNumber(int min, int max) {
        if (min >= max) {
            throw new IllegalArgumentException("最小值必须小于最大值");
        }
        Random random = new Random();
        return random.nextInt((max - min) + 1) + min;
    }
    
    // 生成随机字符串
    public static String generateRandomString(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须大于0");
        }
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(chars.charAt(random.nextInt(chars.length())));
        }
        return sb.toString();
    }
    
    // 验证邮箱格式
    public static boolean isValidEmail(String email) {
        if (email == null || email.isEmpty()) {
            return false;
        }
        String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
        Pattern pattern = Pattern.compile(emailRegex);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }
    
    // 验证手机号格式（中国手机号）
    public static boolean isValidPhone(String phone) {
        if (phone == null || phone.isEmpty()) {
            return false;
        }
        String phoneRegex = "^1[3-9]\\d{9}$";
        Pattern pattern = Pattern.compile(phoneRegex);
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }
    
    // 安全地处理空字符串
    public static String safeString(String str, String defaultValue) {
        return str == null || str.trim().isEmpty() ? defaultValue : str.trim();
    }
    
    // 安全地处理空字符串，默认返回空字符串
    public static String safeString(String str) {
        return safeString(str, "");
    }
    
    // 休眠指定的毫秒数
    public static void sleep(long milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e) {
            logger.error("休眠被中断", e);
            Thread.currentThread().interrupt();
        }
    }
    
    // 计算两个日期之间的天数差
    public static long daysBetween(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            throw new IllegalArgumentException("日期不能为空");
        }
        long diffTime = endDate.getTime() - startDate.getTime();
        return diffTime / (1000 * 60 * 60 * 24);
    }
}